package yunjiao.javatutorials.designpatterns.builder.meal;

import lombok.Getter;
import yunjiao.javatutorials.designpatterns.builder.packing.MultiPack;
import yunjiao.javatutorials.designpatterns.builder.packing.Packing;


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 餐
 *
 * @author yangyunjiao
 */
public class Meal  implements FoodItem {
    @Getter
    private final List<FoodItem> foodItems;

    private final String mealName;

    private final double discount;

    public Meal(String mealName, List<FoodItem> foodItems, double discount) {
        super();
        if (Objects.isNull(foodItems) || foodItems.stream().filter(Objects::nonNull).collect(Collectors.toList()).isEmpty()) {
            throw new IllegalArgumentException("Meal can't be order without any food item");

        }
        this.mealName = mealName;
        this.foodItems = new ArrayList<FoodItem>(foodItems);
        this.discount = discount;
    }

    @Override
    public String name() {
        return mealName;
    }

    @Override
    public int calories() {
        return foodItems.stream().mapToInt(FoodItem::calories).sum();
    }

    @Override
    public Packing packing() {
        double packingPrice = foodItems.stream().map(foodItem -> foodItem.packing())
                .mapToDouble(packing -> packing.packingPrice()).sum();
        return new MultiPack(packingPrice);
    }

    @Override
    public double price() {
        return foodItems.stream().mapToDouble(FoodItem::price).sum();
    }

    public double discount() {
        return discount;
    }
}
